package com.example.rubbish.DataBase;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

public class RDataBase {
    public static  final String INFO_DB_NAME="Record.db";
    private static final int VERSION = 1;
    private static RDataBase RDataBase;
    private static SQLiteDatabase db;

    private RDataBase(Context context) {

        DataBaseHelper dataBaseHelper = new DataBaseHelper(context,INFO_DB_NAME,null, VERSION);
        db=dataBaseHelper.getWritableDatabase();
    }
    public synchronized static RDataBase getInstance(Context context) {
        if(RDataBase ==null){
            RDataBase =new RDataBase(context);
        }

        return RDataBase;
    }

    public boolean haveResult(String garbageName){
        Cursor cursor = db.query("record",
                null,
                "garbage_name=?",
                new String[]{garbageName},
                null,
                null,
                null);
        if(cursor.moveToFirst()){
            cursor.close();
            return  true;
        }
        cursor.close();
        return false;
    }

    //保存GarbageInfo数据到数据库
    public  void saveResult(Record record){
        if(record!=null) {
            ContentValues values = new ContentValues();
            values.put("garbage_name",record.getGarbageName());
            values.put("cate_name",record.getCateName());
            values.put("time",record.getTime());
//            values.put("confidence",garbageInfo.getConfidence());
            db.insert("record",null,values);
        }
    }//saveGarbageInfo()

    public Record getRecord(String name) {

        Cursor cursor = db.query("record",
                null,
                "garbage_name=?",
                new String[]{name},
                null,
                null,
                null);
        if(cursor.moveToFirst()){
            int i_garbage_name= cursor.getColumnIndex("garbage_name");
            int i_id=cursor.getColumnIndex( "id");
            int i_cate_name =cursor.getColumnIndex("cate_name");
            int i_time= cursor.getColumnIndex("time");

            Record record = new Record();
            record.setId(cursor.getInt(i_id));
            record.setCateName(cursor.getString(i_cate_name));
            record.setTime(cursor.getString(i_time));

            record.setGarbageName(cursor.getString(i_garbage_name));
            cursor.close();
            return  record;
        }else {
            cursor.close();
            return  null;
        }

    }


    //从数据库中读取GarbageInfo数据
    public List<Record> loadRecord(){
        List<Record> list=new ArrayList<>();
        int i_id;
        int i_cate_name;
        int i_garbage_name;
        int i_time;


        Cursor cursor = db.query("record",
                null,
                null,
                null,
                null,
                null,
                null);
        if(cursor.moveToFirst()){
            do{
                Record record = new Record();
                i_id=cursor.getColumnIndex( "id");
                i_cate_name =cursor.getColumnIndex("cate_name");
                i_garbage_name= cursor.getColumnIndex("garbage_name");
                i_time= cursor.getColumnIndex("time");


                record.setId(cursor.getInt(i_id));
                record.setCateName(cursor.getString(i_cate_name));
                record.setGarbageName(cursor.getString(i_garbage_name));
                record.setTime(cursor.getString(i_time));
                ///add!!!!!!
                list.add(record);

            }while(cursor.moveToNext());
        }
        cursor.close();
        return list;
    }

    public void remove(Record record){
        String content = record.getGarbageName();
        db.delete("record", "garbage_name = ?", new String[] {content});
    }

    public void reviseResult(Record record) {
        String name = record.getGarbageName();
        ContentValues values = new ContentValues();
        values.put("garbage_name",record.getGarbageName());
        values.put("cate_name",record.getCateName());
        values.put("time",record.getTime());

        db.delete("record", "garbage_name = ?", new String[] {name});
        db.insert("record",null,values);

    }

    public void del(String name) {

        db.delete("record", "garbage_name = ?", new String[] {name});
    }
}
